package com.ruoyi.logistic.controller;

import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.domain.vo.TableVo;
import com.ruoyi.common.service.TableService;
import com.ruoyi.logistic.domain.*;
import com.ruoyi.logistic.domain.vo.NetworkUndertakeGoRankVo;
import com.ruoyi.logistic.domain.vo.NetworkUndertakeGoVo;
import com.ruoyi.logistic.service.NetworkUndertakeGoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequestMapping("/network/undertakeGo")
@RestController
@Api(tags = "网点财务_承业流向结构分析")
public class NetworkUndertakeGoController {
    @Autowired
    private NetworkUndertakeGoService networkUndertakeGoService;
    @Autowired
    private TableService tableService;

    @ApiOperation("显示承/业流向明细列表")
    @GetMapping("/selectDetailAll")
    public R selectAll(PageDate pageDate){
        List<NetworkUndertakeGoDetail> undertakeGoDetailList = networkUndertakeGoService.selectAll(pageDate);
        if (undertakeGoDetailList == null || undertakeGoDetailList.size() == 0){
            return R.fail("暂无数据");
        }else {
            return R.ok(PageInfo.info(undertakeGoDetailList));
        }
    }
    @ApiOperation("添加承/业流向明细")
    @PostMapping("/insertDetail")
    public R insert(@RequestBody NetworkUndertakeGoDetail networkUndertakeGoDetail){
        Boolean isAdd = networkUndertakeGoService.insert(networkUndertakeGoDetail);
        if (isAdd){
            return R.ok("添加成功");
        }else {
            return R.fail("添加失败");
        }
    }
    @ApiOperation("修改承/业流向明细")
    @PostMapping("/updateDetail")
    public R update(@RequestBody NetworkUndertakeGoDetail networkUndertakeGoDetail){
        Boolean isUpdate = networkUndertakeGoService.update(networkUndertakeGoDetail);
        if (isUpdate){
            return R.ok("修改成功");
        }else {
            return R.fail("修改失败");
        }
    }
    @ApiOperation("删除承/业流向明细")
    @PostMapping("/deleteDetailById")
    public R deleteById(Integer id){
        Boolean isDelete = networkUndertakeGoService.deleteById(id);
        if (isDelete){
            return R.ok("删除成功");
        }else {
            return R.fail("删除失败");
        }
    }
    @ApiOperation("搜索")
    @PostMapping("/searchDetail")
    public R searchDetail(@RequestBody NetworkUndertakeGoVo vo){
        List<NetworkUndertakeGoDetail> undertakeGoDetailList = networkUndertakeGoService.search(vo);
        List<TableVo> fieldList = tableService.getFieldAnnotation(new NetworkUndertakeGoDetail());
        JSONObject jsonObject = new com.alibaba.fastjson.JSONObject();
        jsonObject.put("list", PageInfo.info(undertakeGoDetailList));
        jsonObject.put("fields", fieldList);
        if (undertakeGoDetailList == null || undertakeGoDetailList.size() == 0) {
            return R.fail("暂无数据");
        } else {
            return R.ok(jsonObject);
        }
    }

    @ApiOperation("汇总")
    @PostMapping("/collectDetail")
    public R collectDetail(@RequestBody NetworkUndertakeGoVo vo){
        List<NetworkUndertakeGoCollect> undertakeGoCollectList = networkUndertakeGoService.collectUndertakeGo(vo);
        List<TableVo> fieldList = tableService.getFieldAnnotation(new NetworkUndertakeGoCollect());
        JSONObject jsonObject = new com.alibaba.fastjson.JSONObject();
        jsonObject.put("list", PageInfo.info(undertakeGoCollectList));
        jsonObject.put("fields", fieldList);
        if (undertakeGoCollectList == null || undertakeGoCollectList.size() == 0) {
            return R.fail("暂无数据");
        } else {
            return R.ok(jsonObject);
        }
    }

    @ApiOperation("目的省份收入总和排名")
    @PostMapping("/provinceIncomeRank")
    public R provinceIncomeRank(@RequestBody NetworkUndertakeGoRankVo vo){
        List<NetworkUndertakeGoProvinceIncomeRank> provinceIncomeRankList = networkUndertakeGoService.provinceIncomeRank(vo);
        if (provinceIncomeRankList == null || provinceIncomeRankList.size() == 0){
            return R.fail("暂无数据");
        }else {
            return R.ok(provinceIncomeRankList);
        }
    }

    @ApiOperation("承包区在各目的省份收入总和排名")
    @PostMapping("/settleObjectNameIncomeRank")
    public R settleObjectNameIncomeRank(@RequestBody NetworkUndertakeGoRankVo vo){
        NetworkUndertakeGoSettleObjectNameIncomeRankAll settleObjectNameIncomeRanks = networkUndertakeGoService.settleObjectNameIncomeRank(vo);
        if (settleObjectNameIncomeRanks == null){
            return R.fail("暂无数据");
        }else {
            return R.ok(settleObjectNameIncomeRanks);
        }
    }
}
